#region Copyright 2006 Robert McLaws. Licensed under Creative Commons.

// This code is the original work of Robert McLaws. You may modify the code
// to suit your purposes, but please leave this header here. Thank you.
//
// Author: Robert W. McLaws
// Version: 1.0.60822
// Last Updated: 22 August 2006, 2:22 PM
// Updates: http://www.robertmclaws.com/files/folders/cshandlers/entry106.aspx

#endregion

#region Includes

using System;
using System.IO;
using System.Text;
using System.Web;
using System.Xml;
using CommunityServer.Blogs.Components;
using CommunityServer.Configuration;
using CommunityServer.Components;
using CommunityServerStuff.Utilities;
using CommunityServerStuff.Writers;

#endregion

namespace CommunityServerStuff.Handlers
{
    /// <summary>
    /// 
    /// </summary>
    public class ReallySimpleDiscoveryHandler : IHttpHandler
    {

        #region Private Members

        protected HttpContext _context;
        
        #endregion

        #region Properties

        #region IsReusable

        /// <summary>
        /// 
        /// </summary>
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        #endregion

        #region IfModifiedSinceHeader

        /// <summary>
        /// 
        /// </summary>
        protected string IfModifiedSinceHeader
        {
            get
            {
                return _context.Request.Headers["If-Modified-Since"];
            }
        }

        #endregion

        #region IfNoneMatchHeader

        protected string IfNoneMatchHeader
        {
            get
            {
                return _context.Request.Headers["If-None-Match"];
            }
        }

        #endregion

        #endregion

        #region Methods

        #region WriteNotModifiedResponse

        /// <summary>
        /// 
        /// </summary>
        private void WriteNotModifiedResponse()
        {
            _context.Response.StatusCode = 304;
            _context.Response.Status = "304 Not Modified";
        }

        #endregion

        #region ProcessRequest

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentEncoding = Encoding.UTF8;
            context.Response.ContentType = "text/xml";
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            ReallySimpleDiscoveryWriter doc = new ReallySimpleDiscoveryWriter(CSContext.Current.SiteSettings, new EncodedStringWriter(new StringBuilder(), Encoding.UTF8));
            context.Response.Write(doc.ToString());
        }

        #endregion

        #endregion
        
        #region ReallySimpleDiscoveryWriter Internal Class

        /// <summary>
        /// 
        /// </summary>
        internal class ReallySimpleDiscoveryWriter : WriterBase
        {

            #region Methods

            #region Default Constructor

            /// <summary>
            /// 
            /// </summary>
            /// <param name="siteSettings"></param>
            /// <param name="sw"></param>
            internal ReallySimpleDiscoveryWriter(SiteSettings siteSettings, StringWriter sw) : base(siteSettings, sw)
            {
            }

            #endregion

            #region Build

            /// <summary>
            /// 
            /// </summary>
            public override void Build()
            {

                this.Formatting = Formatting.Indented;
                this.WriteStartDocument();
                this.WriteStartElement("rsd");
                this.WriteAttributeString("version", "1.0");
                this.WriteAttributeString("xmlns", "http://archipelago.phrasewise.com/rsd");
                this.WriteStartElement("service");
                this.WriteElementString("engineName", "Community Server");
                this.WriteElementString("engineLink", "http://communityserver.org");
                this.WriteElementString("homePageLink", base.SiteSettings.SiteUrl);
                this.WriteStartElement("apis");
                this.WriteStartElement("api");
                this.WriteAttributeString("name", "Metaweblog");
                this.WriteAttributeString("preferred", "true");
                this.WriteAttributeString("apiLink", base.SiteSettings.SiteUrl + "/blogs/metablog.ashx");
                this.WriteAttributeString("blogID", base.GetCurrentWeblog.ApplicationKey);
                this.WriteEndElement();
                this.WriteEndElement();
                this.WriteEndElement();
                this.WriteEndElement();
                this.WriteEndDocument();
            }

            #endregion

            #endregion

        }

        #endregion

    }
}